package com.ml.mall.utils;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @description    :文件上传工具类
 * @author         :zxm
 * @date           :2021/5/12
**/
public class FileUploadUtils {
    // 指定时间格式
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    // 项目根路径下的目录  -- SpringBoot static 目录相当于是根路径下（SpringBoot 默认） static/upload/imgs
    public final static String IMG_PATH_PREFIX = "../../web-static/static";

    /**
     * 生成文件夹名称
     * @return
     */
    public static String getDirNameByCurrentDate(){
        String dirName = sdf.format(new Date());
        return dirName;
    }

    /**
     * 生成文件名
     */
    public static String getNewNameByUUID(String oldName){
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String newName = UUID.randomUUID().toString().replace("-","").toUpperCase()+suffix;
        return newName;
    }

    /**
     * 	删除用户原先上传的图片
     */
    public static void deleteFile(File file) {
//        File file = new File(uploadRootPath,imgpath);
        if(file.exists()) {
            file.delete();
        }
    }

    /**
     * @description    :description
     * @author         :zxm
     * @date           :2021/5/12
    **/
    public static File getImgDirFile(){
        // 构建上传文件的存放 "文件夹" 路径
//        String fileDirPath = new String("src/main/resources/" + IMG_PATH_PREFIX);
        String fileDirPath = IMG_PATH_PREFIX;

        File fileDir = new File(fileDirPath);
        if(!fileDir.exists()){
            // 递归生成文件夹
            fileDir.mkdirs();
        }
        return fileDir;
    }
}
